package com.why.websocketmqttstudy.why.websocket.handler;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import org.springframework.stereotype.Component;

@Component
public class ServerHeartBeat extends ChannelInboundHandlerAdapter {

//    private static final Logger logger = LoggerFactory.getLogger(ServerHeartBeat.class);
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {//超时事件
            System.out.println("心跳检测超时");
            IdleStateEvent idleEvent = (IdleStateEvent) evt;
            if (idleEvent.state() == IdleState.READER_IDLE) {//读
                ChannelHandlerPool.removeChannel(ctx.channel());
                ctx.channel().close();  //关闭通道连接
//                logger.info("============"+ChannelHandlerPool.findUserCodeByChannel(ctx.channel())+"连接超时，通道关闭=======");
            } else if (idleEvent.state() == IdleState.WRITER_IDLE) {//写

            } else if (idleEvent.state() == IdleState.ALL_IDLE) {//全部

            }
        }
        super.userEventTriggered(ctx, evt);
    }
}

